<template>
  <div class="modern-list-container">
    <!-- 搜索和操作区域 -->
    <div v-if="showFlag" class="modern-list-content">
      <el-form :inline="true" :model="searchForm" class="modern-toolbar">
        <el-row class="toolbar-actions">
          <el-form-item class="action-group">
            <!-- 新增按钮组 -->
            <el-tooltip content="新增" placement="top" v-if="isAuth('config','新增')">
              <el-button
                type="success"
                :icon="contents.btnAdAllIcon ? 'el-icon-plus' : ''"
                class="modern-action-btn"
                @click="addOrUpdateHandler()"
                :class="{'icon-only': !contents.btnAdAllFont}"
              >
                <span v-if="contents.btnAdAllFont">新增</span>
              </el-button>
            </el-tooltip>

            <!-- 删除按钮组 -->
            <el-tooltip content="删除" placement="top" v-if="isAuth('config','删除') && contents.tableSelection">
              <el-button
                type="danger"
                :icon="contents.btnAdAllIcon ? 'el-icon-delete' : ''"
                class="modern-action-btn"
                :disabled="dataListSelections.length <= 0"
                @click="deleteHandler()"
                :class="{'icon-only': !contents.btnAdAllFont}"
              >
                <span v-if="contents.btnAdAllFont">删除</span>
              </el-button>
            </el-tooltip>
          </el-form-item>
        </el-row>
      </el-form>

      <!-- 数据表格 -->
      <div class="modern-table-wrapper">
        <el-table
          :data="dataList"
          class="modern-data-table"
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandler"
          style="width: 100%"
        >
          <el-table-column 
            v-if="contents.tableSelection"
            type="selection"
            width="48"
            align="center"
            header-align="center"
          ></el-table-column>

          <el-table-column 
            label="索引" 
            v-if="contents.tableIndex" 
            type="index" 
            width="64"
            align="center"
            header-align="center"
          >
            <template slot-scope="scope">
              <span class="index-badge">{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>

          <el-table-column 
            prop="name"
            label="名称"
            :sortable="contents.tableSortable"
            :align="contents.tableAlign"
            header-align="center"
            width="300"
          >
            <template slot-scope="scope">
              <div class="table-cell">
                <i class="el-icon-document"></i>
                <span class="text-ellipsis">{{ scope.row.name }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column 
            prop="value"
            label="图片"
            :sortable="contents.tableSortable"
            :align="contents.tableAlign"
            header-align="center"
          
          >
            <template slot-scope="scope">
              <div class="image-preview">
                <el-image
                  v-if="scope.row.value"
                  :src="$base.url+scope.row.value.split(',')[0]"
                  :preview-src-list="[scope.row.value]"
                  fit="cover"
                  class="preview-image"
                >
                  <div slot="error" class="image-error">
                    <i class="el-icon-picture-outline"></i>
                  </div>
                </el-image>
                <span v-else class="empty-text">无图片</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column 
            label="操作" 
            width="300" 
            align="center" 
            header-align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="table-actions">
                <!-- 详情按钮 -->
                <el-tooltip content="详情" placement="top" v-if="isAuth('config','查看')">
                  <el-button
                    type="text"
                    :icon="contents.tableBtnIcon ? 'el-icon-tickets' : ''"
                    class="action-btn"
                    @click="addOrUpdateHandler(scope.row.id,'info')"
                  >
                    <span v-if="contents.tableBtnFont">详情</span>
                  </el-button>
                </el-tooltip>

                <!-- 修改按钮 -->
                <el-tooltip content="修改" placement="top" v-if="isAuth('config','修改')">
                  <el-button
                    type="text"
                    :icon="contents.tableBtnIcon ? 'el-icon-edit' : ''"
                    class="action-btn"
                    @click="addOrUpdateHandler(scope.row.id)"
                  >
                    <span v-if="contents.tableBtnFont">修改</span>
                  </el-button>
                </el-tooltip>

                <!-- 删除按钮 -->
                <el-tooltip content="删除" placement="top" v-if="isAuth('config','删除')">
                  <el-button
                    type="text"
                    :icon="contents.tableBtnIcon ? 'el-icon-delete' : ''"
                    class="action-btn"
                    @click="deleteHandler(scope.row.id)"
                  >
                    <span v-if="contents.tableBtnFont">删除</span>
                  </el-button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="modern-pagination">
          <el-pagination
            background
            :page-sizes="[10, 20, 50, 100]"
            :page-size="Number(contents.pageEachNum)"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalPage"
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
          ></el-pagination>
        </div>
      </div>
    </div>

    <!-- 添加/修改弹窗 -->
    <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>
  </div>
</template>
<script>
import AddOrUpdate from "./add-or-update.vue";
import styleJs from "../../../utils/style.js";
import utilsJs, {getYearFormat,getMonthFormat,getDateFormat,getDatetimeFormat} from "../../../utils/utils.js";
export default {
  data() {
    return {
      searchForm: {
        key: ""
      },
      form:{},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      addOrUpdateFlag:false,
      contents:null,
      layouts: '',


    };
  },
  created() {
    this.contents = styleJs.listStyle();
    this.init();
    this.getDataList();
    this.contentStyleChange()
  },
  mounted() {

  },
  filters: {
    htmlfilter: function (val) {
      return val.replace(/<[^>]*>/g).replace(/undefined/g,'');
    }
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    contentStyleChange() {
      this.contentSearchStyleChange()
      this.contentBtnAdAllStyleChange()
      this.contentSearchBtnStyleChange()
      this.contentTableBtnStyleChange()
      this.contentPageStyleChange()
    },
    contentSearchStyleChange() {
      this.$nextTick(()=>{
        document.querySelectorAll('.form-content .slt .el-input__inner').forEach(el=>{
          let textAlign = 'left'
          if(this.contents.inputFontPosition == 2) textAlign = 'center'
          if(this.contents.inputFontPosition == 3) textAlign = 'right'
          el.style.textAlign = textAlign
          el.style.height = this.contents.inputHeight
          el.style.lineHeight = this.contents.inputHeight
          el.style.color = this.contents.inputFontColor
          el.style.fontSize = this.contents.inputFontSize
          el.style.borderWidth = this.contents.inputBorderWidth
          el.style.borderStyle = this.contents.inputBorderStyle
          el.style.borderColor = this.contents.inputBorderColor
          el.style.borderRadius = this.contents.inputBorderRadius
          el.style.backgroundColor = this.contents.inputBgColor
        })
        if(this.contents.inputTitle) {
          document.querySelectorAll('.form-content .slt .el-form-item__label').forEach(el=>{
            el.style.color = this.contents.inputTitleColor
            el.style.fontSize = this.contents.inputTitleSize
            el.style.lineHeight = this.contents.inputHeight
          })
        }
        setTimeout(()=>{
          document.querySelectorAll('.form-content .slt .el-input__prefix').forEach(el=>{
            el.style.color = this.contents.inputIconColor
            el.style.lineHeight = this.contents.inputHeight
          })
          document.querySelectorAll('.form-content .slt .el-input__suffix').forEach(el=>{
            el.style.color = this.contents.inputIconColor
            el.style.lineHeight = this.contents.inputHeight
          })
          document.querySelectorAll('.form-content .slt .el-input__icon').forEach(el=>{
            el.style.lineHeight = this.contents.inputHeight
          })
        },10)

      })
    },
    // 搜索按钮
    contentSearchBtnStyleChange() {
      this.$nextTick(()=>{
        document.querySelectorAll('.form-content .slt .el-button--success').forEach(el=>{
          el.style.height = this.contents.searchBtnHeight
          el.style.color = this.contents.searchBtnFontColor
          el.style.fontSize = this.contents.searchBtnFontSize
          el.style.borderWidth = this.contents.searchBtnBorderWidth
          el.style.borderStyle = this.contents.searchBtnBorderStyle
          el.style.borderColor = this.contents.searchBtnBorderColor
          el.style.borderRadius = this.contents.searchBtnBorderRadius
          el.style.backgroundColor = this.contents.searchBtnBgColor
        })
      })
    },
    // 新增、批量删除
    contentBtnAdAllStyleChange() {
      this.$nextTick(()=>{
        document.querySelectorAll('.form-content .ad .el-button--success').forEach(el=>{
          el.style.height = this.contents.btnAdAllHeight
          el.style.color = this.contents.btnAdAllAddFontColor
          el.style.fontSize = this.contents.btnAdAllFontSize
          el.style.borderWidth = this.contents.btnAdAllBorderWidth
          el.style.borderStyle = this.contents.btnAdAllBorderStyle
          el.style.borderColor = this.contents.btnAdAllBorderColor
          el.style.borderRadius = this.contents.btnAdAllBorderRadius
          el.style.backgroundColor = this.contents.btnAdAllAddBgColor
        })
        document.querySelectorAll('.form-content .ad .el-button--danger').forEach(el=>{
          el.style.height = this.contents.btnAdAllHeight
          el.style.color = this.contents.btnAdAllDelFontColor
          el.style.fontSize = this.contents.btnAdAllFontSize
          el.style.borderWidth = this.contents.btnAdAllBorderWidth
          el.style.borderStyle = this.contents.btnAdAllBorderStyle
          el.style.borderColor = this.contents.btnAdAllBorderColor
          el.style.borderRadius = this.contents.btnAdAllBorderRadius
          el.style.backgroundColor = this.contents.btnAdAllDelBgColor
        })
        document.querySelectorAll('.form-content .ad .el-button--warning').forEach(el=>{
          el.style.height = this.contents.btnAdAllHeight
          el.style.color = this.contents.btnAdAllWarnFontColor
          el.style.fontSize = this.contents.btnAdAllFontSize
          el.style.borderWidth = this.contents.btnAdAllBorderWidth
          el.style.borderStyle = this.contents.btnAdAllBorderStyle
          el.style.borderColor = this.contents.btnAdAllBorderColor
          el.style.borderRadius = this.contents.btnAdAllBorderRadius
          el.style.backgroundColor = this.contents.btnAdAllWarnBgColor
        })
      })
    },
    // 表格
    rowStyle({ row, rowIndex}) {
      if (rowIndex % 2 == 1) {
        if(this.contents.tableStripe) {
          return {color:this.contents.tableStripeFontColor}
        }
      } else {
        return ''
      }
    },
    cellStyle({ row, rowIndex}){
      if (rowIndex % 2 == 1) {
        if(this.contents.tableStripe) {
          return {backgroundColor:this.contents.tableStripeBgColor}
        }
      } else {
        return ''
      }
    },
    headerRowStyle({ row, rowIndex}){
      return {color: this.contents.tableHeaderFontColor}
    },
    headerCellStyle({ row, rowIndex}){
      return {backgroundColor: this.contents.tableHeaderBgColor}
    },
    // 表格按钮
    contentTableBtnStyleChange(){
      // this.$nextTick(()=>{
      //   setTimeout(()=>{
      //     document.querySelectorAll('.table-content .tables .el-table__body .el-button--success').forEach(el=>{
      //       el.style.height = this.contents.tableBtnHeight
      //       el.style.color = this.contents.tableBtnDetailFontColor
      //       el.style.fontSize = this.contents.tableBtnFontSize
      //       el.style.borderWidth = this.contents.tableBtnBorderWidth
      //       el.style.borderStyle = this.contents.tableBtnBorderStyle
      //       el.style.borderColor = this.contents.tableBtnBorderColor
      //       el.style.borderRadius = this.contents.tableBtnBorderRadius
      //       el.style.backgroundColor = this.contents.tableBtnDetailBgColor
      //     })
      //     document.querySelectorAll('.table-content .tables .el-table__body .el-button--primary').forEach(el=>{
      //       el.style.height = this.contents.tableBtnHeight
      //       el.style.color = this.contents.tableBtnEditFontColor
      //       el.style.fontSize = this.contents.tableBtnFontSize
      //       el.style.borderWidth = this.contents.tableBtnBorderWidth
      //       el.style.borderStyle = this.contents.tableBtnBorderStyle
      //       el.style.borderColor = this.contents.tableBtnBorderColor
      //       el.style.borderRadius = this.contents.tableBtnBorderRadius
      //       el.style.backgroundColor = this.contents.tableBtnEditBgColor
      //     })
      //     document.querySelectorAll('.table-content .tables .el-table__body .el-button--danger').forEach(el=>{
      //       el.style.height = this.contents.tableBtnHeight
      //       el.style.color = this.contents.tableBtnDelFontColor
      //       el.style.fontSize = this.contents.tableBtnFontSize
      //       el.style.borderWidth = this.contents.tableBtnBorderWidth
      //       el.style.borderStyle = this.contents.tableBtnBorderStyle
      //       el.style.borderColor = this.contents.tableBtnBorderColor
      //       el.style.borderRadius = this.contents.tableBtnBorderRadius
      //       el.style.backgroundColor = this.contents.tableBtnDelBgColor
      //     })

      //   }, 50)
      // })
    },
    // 分页
    contentPageStyleChange(){
      let arr = []

      if(this.contents.pageTotal) arr.push('total')
      if(this.contents.pageSizes) arr.push('sizes')
      if(this.contents.pagePrevNext){
        arr.push('prev')
        if(this.contents.pagePager) arr.push('pager')
        arr.push('next')
      }
      if(this.contents.pageJumper) arr.push('jumper')
      this.layouts = arr.join()
      this.contents.pageEachNum = 10
    },

    init () {
    },
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: 'id',
      }
          if(this.searchForm.name!='' && this.searchForm.name!=undefined){
            params['name'] = '%' + this.searchForm.name + '%'
          }
      this.$http({
        url: "config/page",
        method: "get",
        params: params
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id,type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if(type!='info'){
        type = 'else';
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id,type);
      });
    },
    // 查看评论
    // 下载
    download(file){
      window.open(`${file}`)
    },
    // 删除
    deleteHandler(id) {
      var ids = id
        ? [Number(id)]
        : this.dataListSelections.map(item => {
            return Number(item.id);
          });
      this.$confirm(`确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: "config/delete",
          method: "post",
          data: ids
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.search();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  }

};
</script>
<style scoped>
.modern-list-container {
  --primary-color: #409EFF;
  --success-color: #67C23A;
  --danger-color: #F56C6C;
  --warning-color: #E6A23C;
  --border-color: #EBEEF5;
  --bg-base: #F5F7FA;
  --radius-md: 8px;
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --text-primary: #303133;
  --text-secondary: #606266;
}

.modern-list-content {
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(8px);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  border: 1px solid var(--border-color);
}

.modern-toolbar {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border-color);
}

.toolbar-actions {
  display: flex;
  justify-content: flex-end;
}

.action-group {
  margin: 0;
  display: flex;
  gap: 12px;
}

.modern-action-btn {
  padding: 10px 18px;
  border-radius: 20px;
  transition: all 0.3s ease;
}

.modern-action-btn.icon-only {
  width: 40px;
  padding: 10px;
}

.modern-action-btn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.modern-table-wrapper {
  padding: 0 24px 24px;
}

.modern-data-table::v-deep {
  --el-table-header-background-color: rgba(245, 247, 250, 0.9);
  --el-table-border-color: var(--border-color);
}

.modern-data-table::v-deep th {
  background: var(--el-table-header-background-color);
  backdrop-filter: blur(4px);
  font-weight: 600;
  color: var(--text-primary);
}

.modern-data-table::v-deep tr {
  transition: all 0.2s ease;
}

.modern-data-table::v-deep tr:hover {
  transform: translateX(4px);
  background: rgba(64, 158, 255, 0.03);
  box-shadow: 4px 0 12px rgba(64, 158, 255, 0.05);
}

.index-badge {
  display: inline-block;
  width: 28px;
  height: 28px;
  line-height: 28px;
  border-radius: 50%;
  background: rgba(64, 158, 255, 0.1);
  color: var(--primary-color);
  font-weight: 500;
}

.table-cell {
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
  gap: 8px;
  color: var(--text-primary);
}

.text-ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-preview {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 80px;
}

.preview-image {
  width: 80px;
  height: 80px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  transition: all 0.3s ease;
}

.preview-image:hover {
  transform: scale(1.05);
  box-shadow: var(--shadow-md);
}

.empty-text {
  color: var(--text-secondary);
  font-size: 14px;
}

.table-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.action-btn {
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.action-btn:hover {
  background: rgba(64, 158, 255, 0.08);
  color: var(--primary-color);
}

.modern-pagination {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
}

@media (max-width: 768px) {
  .modern-toolbar {
    padding: 12px;
  }
  
  .toolbar-actions {
    justify-content: flex-start;
  }
  
  .modern-data-table::v-deep td {
    padding: 12px 8px;
  }
  
  .table-actions {
    flex-wrap: wrap;
  }
  
  .preview-image {
    width: 60px;
    height: 60px;
  }
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.modern-list-content {
  animation: fadeIn 0.3s ease;
}
</style>